﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using DBUtility.PetaPoco;
using Model;

namespace DAL
{
	/// <summary>
    /// 设备 數據訪問類
    /// </summary>
    public class DeviceDAL : BaseDAL<Model.Device>
    {
        /// <summary>
        /// 獲取相關類型和消息的設備號碼
        /// </summary>
        /// <param name="Type">類型</param>
        /// <param name="Mid">消息編號</param>
        /// <returns></returns>
        public string[] GetDeviceList(int Type, int Mid)
        {

            List<Device> list = db.Fetch<Device>("where Type=@0 and Mid=@1 and Status=@2 and Count<=3", Type, Mid, false);
            string[] str = new string[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                str[i] = list[i].Devicetoken;
            }
            return str;
        }

        /// <summary>
        /// 改變設備推送狀態
        /// </summary>
        /// <param name="MId">消息編號</param>
        /// <param name="token">設備編號</param>
        /// <returns></returns>
        public int UpDateStatus(int mId,string token)
        {
            try
            {
                return db.Execute("update device set Status=true where Mid=@0 and Devicetoken=@1", mId, token);
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        /// <summary>
        /// 推送失败时加上次数
        /// </summary>
        /// <param name="mId">消息編號</param>
        /// <param name="token">設備編號</param>
        /// <returns></returns>
        public int UpdateCount(int mId, string token)
        {
            return db.Execute("update device set Count=Count+1 where Mid=@0 and Devicetoken=@1", mId, token);
        }
    }
}
